﻿'use strict';

// Google Analytics Collection APIs Reference:
// https://developers.google.com/analytics/devguides/collection/analyticsjs/

angular.module('app.controllers', [])

    // Path: /
    .controller('HomeCtrl', ['$scope', '$location', '$window', function ($scope, $location, $window) {
        $scope.$root.title = 'AngularJS SPA Template for Visual Studio';
        $scope.$on('$viewContentLoaded', function () {
            $window.ga('send', 'pageview', { 'page': $location.path(), 'title': $scope.$root.title });
        });
    }])

    // Path: /about
    .controller('AboutCtrl', ['$scope', '$location', '$window', function ($scope, $location, $window) {
        $scope.$root.title = 'AngularJS SPA | About';
        $scope.$on('$viewContentLoaded', function () {
            $window.ga('send', 'pageview', { 'page': $location.path(), 'title': $scope.$root.title });
        });
    }])

    // Path: /login
    .controller('LoginCtrl', ['$scope', '$location', '$window', function ($scope, $location, $window) {
        $scope.$root.title = 'AngularJS SPA | Sign In';
        // TODO: Authorize a user
        $scope.login = function () {
            $location.path('/');
            return false;
        };
        $scope.$on('$viewContentLoaded', function () {
            $window.ga('send', 'pageview', { 'page': $location.path(), 'title': $scope.$root.title });
        });
    }])

    // Path: /drinks
    .controller('DrinksCtrl', ['$scope', '$location', '$window', function ($scope, $location, $window, drinksService) {
        $scope.$root.title = 'Drinks';

        // Provide the view to access existing drinks
        $scope.drinks = drinksService.getDrinks();

        // Provide the view to insert a new drink
        $scope.insertDrink = function () {
            drinksService.insertDrink(
                $scope.newDrink.name,
                $scope.newDrink.ingredients,
                $scope.newDrink.garnish,
                $scope.newDrink.description);

            // Reset the view variables
            $scope.newDrink.name = null;
            $scope.newDrink.ingredients = null,
            $scope.newDrink.garnish = null,
            $scope.newDrink.description = null;
        };

        $scope.deleteDrink = function (id) {
            drinksService.deleteDrink(id);
        }

        $scope.$on('$viewContentLoaded', function () {
            $window.ga('send', 'pageview', { 'page': $location.path(), 'title': $scope.$root.title });
        });
    }])

    // Path: /drinks/:id
    .controller('DrinkDetailCtrl', ['$scope', '$location', '$window', function ($scope, $location, $window, $routeParams, drinksService) {

        // Get drink ID off of the route
        var drinkId = parseInt($routeParams.drinkId);

        // Get the drink from the service and associate it with the view
        if (drinkId > 0) {
            $scope.drink = drinksService.getDrink(drinkId);
        }

        $scope.$root.title = $scope.drink.name;
        $scope.$on('$viewContentLoaded', function () {
            $window.ga('send', 'pageview', { 'page': $location.path(), 'title': $scope.$root.title });
        });
    }])

    // Path: /error/404
    .controller('Error404Ctrl', ['$scope', '$location', '$window', function ($scope, $location, $window) {
        $scope.$root.title = 'Error 404: Page Not Found';
        $scope.$on('$viewContentLoaded', function () {
            $window.ga('send', 'pageview', { 'page': $location.path(), 'title': $scope.$root.title });
        });
    }]);